package edu.pku.sei.autounit.generator;

import edu.pku.sei.autounit.core.DefaultVariable;
import edu.pku.sei.autounit.core.Variable;
import edu.pku.sei.autounit.core.VariableFactory;
import edu.pku.sei.autounit.core.VariableGenerator;
import edu.pku.sei.autounit.filter.Filter;
import edu.pku.sei.autounit.reflect.Type;
import edu.pku.sei.autounit.util.NameFactory;
import edu.pku.sei.autounit.util.Properties;
import edu.pku.sei.autounit.util.Statements;

public class PrimitiveGenerator implements VariableGenerator {
	
	public Variable generate(Type type,String name,VariableFactory vf,NameFactory nf) {
		String value = null;
		if(type.getFullName().equals("boolean")){
			if(Math.random()<Properties.getBoolean())
				value = String.valueOf(true);
			else
				value = String.valueOf(false);
		}
		
		else if(type.getFullName().equals("byte")){
			byte temp = (byte)(Math.random()*(Byte.MAX_VALUE-Byte.MIN_VALUE)+Byte.MIN_VALUE);
			value = String.valueOf(temp);
			value = "(byte)"+value;
		}
		
		else if(type.getFullName().equals("char")){
			String s = Properties.getChar();
			int index = (int)(Math.random()*(s.length()-1));
			char c = s.charAt(index);
			if(isSpecialCharacter(c))
				value = "'\\"+c+"'";
			else
				value = "'"+c+"'";
		}
		
		else if(type.getFullName().equals("short")){
			short max = Properties.getMaxShort();
			short min = Properties.getMinShort();
			short temp = (short)(Math.random()*(max-min)+min);
			value = String.valueOf(temp);
			value = "(short)"+value;
		}
		
		else if(type.getFullName().equals("int")){
			int max = Properties.getMaxInt();
			int min = Properties.getMinInt();
			int temp = (int)(Math.random()*(max-min)+min);
			value = String.valueOf(temp);
		}
		
		else if(type.getFullName().equals("long")){
			long max = Properties.getMaxLong();
			long min = Properties.getMinLong();
			long temp = (long)(Math.random()*(max-min)+min);
			value = String.valueOf(temp)+"l";
		}
		
		else if(type.getFullName().equals("float")){
			float max = Properties.getMaxFloat();
			float min = Properties.getMinFloat();
			float temp = (float)(Math.random()*(max-min)+min);
			value = String.valueOf(temp)+"f";
		}
		
		else if(type.getFullName().equals("double")){
			double max = Properties.getMaxDouble();
			double min = Properties.getMinDouble();
			double temp = (Math.random()*(max-min)+min);
			value = String.valueOf(temp);
		}
		
		else
			return null;
		
		return new DefaultVariable(type, new Statements(), value);
	}
	
	private boolean isSpecialCharacter(char c){
		if(c=='\\')
			return true;
		if(c=='\'')
			return true;
		return false;
	}

	
	public Variable generate(Type type, String name, Filter fliter,
			VariableFactory vf, NameFactory nf) {
		// TODO Auto-generated method stub
		return null;
	}
}
